package com.winchain.chainbill.bill.biz.discount.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.SysDctPaidKeyword;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.discount.service.BillDctEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctRevSmsCmd implements ICommand<Bill<CbDct>>, ICallback<Bill<CbDct>, AcctTxRec> {
	@Autowired
	private BillDctEventService billDctEventService;
	@Autowired
	private CbSndService cbSndService;
	@Autowired
	private CbRecService cbRecService;

	public boolean execute(CommandContext<Bill<CbDct>> context) throws Exception {
		CbDct cbDct = (CbDct) ((Bill) context.getSource()).getSource();

		Object timeOut = context.getValue("Timeout");
		if ((timeOut != null) && (((Boolean) timeOut).booleanValue())) {
			return false;
		}
		if ((cbDct != null) && (cbDct.getDctId() != null)) {
			if (cbDct.getSellerId().equals(cbDct.getBuyerId())) {
				sendSelfPayBySms(cbDct);
			} else {
				sendNonSelfPayBySms(cbDct);
			}
		}
		return false;
	}

	private void sendSelfPayBySms(CbDct cbDct) {
		String template = "MT02627";
		SysDctPaidKeyword keyWord = new SysDctPaidKeyword();
		keyWord.setDctAmt(String.valueOf(cbDct.getDctAmt()));
		String dctDate = SysDateUtil.parseDate(cbDct.getDctDate(), "yyyyMMdd", "yyyy-MM-dd");
		keyWord.setDctDate(dctDate);
		keyWord.setContactorCompany(cbDct.getSellerNm());

		String senderId = CommonDataUtil.getUserId();
		MessageSender.sendMessageByTmpl(senderId, template, keyWord, cbDct.getFinId());
	}

	public void sendNonSelfPayBySms(CbDct cbDct) {
		String template = "MT02612";
		SysDctPaidKeyword keyWord = new SysDctPaidKeyword();
		keyWord.setDctAmt(String.valueOf(cbDct.getDctAmt()));
		String dctDate = SysDateUtil.parseDate(cbDct.getDctDate(), "yyyyMMdd", "yyyy-MM-dd");
		keyWord.setDctDate(dctDate);
		keyWord.setContactorCompany(cbDct.getSellerNm());

		String senderId = CommonDataUtil.getUserId();

		MessageSender.sendMessageByTmpl(senderId, template, keyWord, cbDct.getFinId());
	}

	public boolean callback(CallbackContext<Bill<CbDct>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}
